#ifdef BOARDTEST
#include <kt1260.h>
#include <testcase.h>
#include <STM32Ethernet.h>
IPAddress ipp(192, 168, 31, 156);
#include "USBStick.h"
USBStick usb;
#include "lwiperf.h"
void test_di_do();
void test_axy();
void ilwiperf_report_fn(void *arg, enum lwiperf_report_type report_type,
                        const ip_addr_t *local_addr, u16_t local_port, const ip_addr_t *remote_addr, u16_t remote_port,
                        u32_t bytes_transferred, u32_t ms_duration, u32_t bandwidth_kbitpsec) {}
void setup()
{
    Serial1.begin(115200);
    board.begin();
    core_debug("start");
    Ethernet.begin(ipp);

    usb.begin();
    lwiperf_start_tcp_server_default(ilwiperf_report_fn, 0);
}
int flag = 0;
int lasttick = -10000;
int download(const char *host, uint16_t port, const char *url, const char *filename);
void loop()
{
    // core_debug(".");
    // if (millis() - lasttick > 60000)
    // {
    //     UNITY_BEGIN(); // IMPORTANT LINE!
    //     RUN_TEST(test_spiffs);
    //     RUN_TEST(test_di_init);
    //     RUN_TEST(test_di_do);
    //     RUN_TEST(test_axy);
    //     if (UNITY_END() == 0)
    //     {
    //         flag |= 4;
    //     }
    //     lasttick = millis();
    // }
    // if (!(flag & 0x1))
    // {
    //     flag |= download("123.207.157.205", 8018, "/stm32flash.exe", "test.bin") ? 0 : 1;
    //     delay(1000);
    // }
    // if (!(flag & 0x2))
    // {
    //     flag |= download("123.207.157.205", 8018, "/stm32flash.exe", "main.bin") ? 0 : 2;
    //     delay(1000);
    // }
    // if (flag == 0x7)
    // {
    //     FlashFile prog("prog", SPIFFS_O_CREAT | SPIFFS_O_RDWR);
    //     prog.write("test.bin", 8);
    //     prog.close();

    //     ch.setChar('V', 1);
    // }
    usb.loop();
}
void test_di_do()
{
    yy1 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X1), "X1");

    yy2 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X2), "X1");

    yy3 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X3), "X1");

    yy4 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X4), "X1");

    yy5 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X5), "X1");

    yy6 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X6), "X1");

    yy7 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X7), "X1");

    yy8 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X8), "X1");
    yy9 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X9), "X1");

    yy10 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X10), "X1");

    yy11 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X11), "X1");

    yy12 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X12), "X1");

    yy13 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X13), "X1");

    yy14 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X14), "X1");

    yy15 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X15), "X1");

    yy16 = 1;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(LOW, digitalRead(X16), "X1");

    yy1 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X1), "X1");

    yy2 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X2), "X1");

    yy3 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X3), "X1");

    yy4 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X4), "X1");

    yy5 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X5), "X1");

    yy6 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X6), "X1");

    yy7 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X7), "X1");

    yy8 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X8), "X1");
    yy9 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X9), "X1");
    yy10 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X10), "X1");
    yy11 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X11), "X1");
    yy12 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X12), "X1");
    yy13 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X13), "X1");
    yy14 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X14), "X1");
    yy15 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X15), "X1");
    yy16 = 0;
    board.output();
    delay(20);
    TEST_ASSERT_EQUAL_MESSAGE(HIGH, digitalRead(X16), "X1");
}
void test_axy()
{
    ya1 = 1000;
    ya2 = 1000;
    board.output();
    board.input();
    TEST_ASSERT_INT32_WITHIN(50, 2000, xa1);
    TEST_ASSERT_INT32_WITHIN(50, 2000, xa2);
    TEST_ASSERT_INT32_WITHIN(50, 2000, xa3);
    TEST_ASSERT_INT32_WITHIN(50, 2000, xa4);
    TEST_ASSERT_INT32_WITHIN(50, 2000, xa5);
    TEST_ASSERT_INT32_WITHIN(50, 2000, xa6);
    TEST_ASSERT_INT32_WITHIN(50, 2000, xa7);
    TEST_ASSERT_INT32_WITHIN(50, 2000, xa8);
    TEST_ASSERT_INT32_WITHIN(50, 2000, xa9);
    TEST_ASSERT_INT32_WITHIN(50, 2000, xa10);
}
void copy_file(const char *name)
{
    char buf[1024];
    int len;
    if (usb.stat(name) == 0 && usb.open_file_rdonly(name) == 0)
    {
        FlashFile a(name, SPIFFS_O_CREAT | SPIFFS_O_RDWR);
        int s = usb.fsize;
        while (s > 0)
        {
            len = usb.read(buf, 1024);
            core_debug("A: len=%d \n", len);
            a.write(buf, len);
            s -= len;
        }
        a.close();
        usb.close();
#define TEST_BACK 1
#if TEST_BACK
        FlashFile b(name, SPIFFS_O_RDONLY);
        s = b.size();
        char fname[16];
        sprintf(fname, "%s_b", name);
        usb.open_file(fname);
        while (s > 0)
        {
            len = b.read(buf, 1024);
            if(!len)break;
            core_debug("A: len=%d s=%d\n", len,s);
            usb.write(buf, len);
            s -= len;
        }
        b.close();
        usb.close();

#endif
    }
}
void usb_ready()
{
    copy_file("main.bin");
    copy_file("test.bin");
}
#endif